<script setup>
import {useRoute} from "vue-router"
import {getDocumentOperateMenu} from "@/utils/menu.js";
import {
  confirmReceive,
  getDocReceiveBasicInfo,
  getIsConfirmReceive, getDocReceiveFinalFileInfo, getDocReceiveRecord
} from "@/api/document/exchangeDocument.js";
import DocumentView from "@/views/document/exchange/common/documentView.vue";

const {proxy} = getCurrentInstance()
const activeTabName = ref('documentBasicInfo')
const route = useRoute()
const id = Number(route.params && route.params.id)
const documentInfo = ref({})
const receiveRecord = ref({})
const opMenuEnum = getDocumentOperateMenu()
const isConfirmReceive = ref(false)
const statusType = ref(null)
provide('currentMenu', opMenuEnum.exchange)
const docFinalFileInfo = ref({})
function getDocFinalFileInfo() {
  getDocReceiveFinalFileInfo(id).then((res)=> {
    docFinalFileInfo.value = res
  }).catch((err) => {
    proxy.$modal.alertError(err)
  })
}
getDocFinalFileInfo()
provide('getDocFinalFileInfo', docFinalFileInfo)

onMounted(() => {
  loadMainViewData()
})

function loadMainViewData() {
  if (id) {
    getDocReceiveRecordInfo()
    getDocBasicInfo()
    getButtonsInfo()
  }
}

function getDocReceiveRecordInfo() {
  getDocReceiveRecord(id).then(res => {
    receiveRecord.value = res
    statusType.value = res.status === '待签收' ? 'success' : 'danger'
  })
}

function getDocBasicInfo() {
  getDocReceiveBasicInfo(id).then(res => {
    documentInfo.value = res
  })
}

function getButtonsInfo() {
  getIsConfirmReceive(id).then(res => {
    isConfirmReceive.value = res
  })
}

const confirmReceiveDoc = () => {
  proxy.$modal.confirmAndExecuteJS('确认签收当前公文？', confirmReceive, id, loadMainViewData)
}

</script>

<template>
  <div class="app-container">
    <el-card :span="24">
      <template #header>
        <el-row style="line-height: 40px">
          <el-text type="primary" style="font-size: 18px;">{{ documentInfo.subject }}</el-text>
          <el-text class="ml10">
            <el-tag type="info" effect="plain" class="ml10">收文单位：{{ receiveRecord.receiveDeptName }}</el-tag>
            <el-tag type="info" effect="plain" class="ml10">收文用户：{{ receiveRecord.receiveUserName }}</el-tag>
            <el-tag type="info" effect="plain" class="ml10">收文来源：{{ receiveRecord.receiveSource }}</el-tag>
            <el-tag :type="statusType" effect="plain" class="ml10">签收状态：{{ receiveRecord.status }}</el-tag>
            <el-tag :type="statusType" effect="plain" class="ml10" v-if="receiveRecord.confirmReceiveTime">签收时间：{{ receiveRecord.confirmReceiveTime }}</el-tag>
          </el-text>
        </el-row>
      </template>
      <el-row>
        <div class="el-table el-table--enable-row-hover el-table--medium">
          <DocumentView :documentInfo="documentInfo" :recordId="id">
            <el-button type="success" @click="confirmReceiveDoc" v-if="isConfirmReceive">签收公文</el-button>
          </DocumentView>
        </div>
      </el-row>
    </el-card>
  </div>
</template>
